package com.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.PaySystemConstant;
import com.example.auth.domain.DataNode;
import com.example.auth.domain.User;
import com.example.config.exception.GlobalExcetion;
import com.example.feign.NodeFeignService;
import com.example.feign.UserFeignService;
import com.example.pay.domain.UserMoneyAccount;
import com.example.pay.domain.UserPayRecordIn;
import com.example.service.UserMoneyAccountService;
import com.example.mapper.UserMoneyAccountMapper;
import com.example.service.UserPayRecordInService;
import com.example.system.domain.BusSystemTableUser;
import com.example.system.service.BusSystemTableService;
import com.example.system.service.BusSystemTableUserService;
import com.example.util.BigDecimalUtils;
import com.example.util.sql.DBSql;
import com.example.util.sql.SqlGenerator;
import com.example.util.sql.SqlResult;
import com.example.vo.UserMoneyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.math.BigDecimal;
import java.util.Date;

/**
* @author CETC
* @description 针对表【user_money_account_0】的数据库操作Service实现
* @createDate 2025-11-01 18:18:45
*/
@Service
public class UserMoneyAccountServiceImpl extends ServiceImpl<UserMoneyAccountMapper, UserMoneyAccount>
    implements UserMoneyAccountService {

    @Value("${spring.application.name}")
    private String appName;

    @Autowired
    private DataSource dataSource;

    @Autowired
    private BusSystemTableService busSystemTableService;

    @Autowired
    private BusSystemTableUserService busSystemTableUserService;

    @Autowired
    private UserFeignService userFeignService;

    @Autowired
    private NodeFeignService nodeFeignService;

    @Autowired
    private UserPayRecordInService userPayRecordInService;

    @Override
    public UserMoneyAccount saveUserMoneyAccount(String tableName,UserMoneyAccount userMoneyAccount) throws GlobalExcetion {
        DBSql dbSql = new DBSql(dataSource);
        if(userMoneyAccount==null){
            throw new GlobalExcetion(1,"传入的数据不得为空");
        }
        String uuid=busSystemTableService.createUUID(tableName);
        userMoneyAccount.setUuid(uuid);
        userMoneyAccount.setCreateTime(new Date());
        userMoneyAccount.setUpdateTime(new Date());
        SqlResult result=SqlGenerator.generateInsert(tableName, userMoneyAccount);
        dbSql.executeInsert(result.getSql(),result.getParams().toArray());
        return userMoneyAccount;
    }

    @Override
    public UserMoneyAccount updateUserMoneyAccountById(UserMoneyAccount userMoneyAccount) throws GlobalExcetion {
        if(userMoneyAccount==null){
            throw new GlobalExcetion(1,"传入的数据不得为空");
        }
        if(userMoneyAccount.getUuid()==null){
            throw new GlobalExcetion(1,"主键id不得为空");
        }
        String tableName=busSystemTableService.getTableNameByUUID(userMoneyAccount.getUuid());
        userMoneyAccount.setUpdateTime(new Date());
        DBSql dbSql = new DBSql(dataSource);
        SqlResult result=SqlGenerator.generateUpdateById(tableName,userMoneyAccount,"uuid");
        dbSql.executeUpdate(result.getSql(),result.getParams().toArray());
        return userMoneyAccount;
    }

    @Override
    public UserMoneyAccount getUserMoneyAccountById(String id) throws GlobalExcetion {
        String tableName=busSystemTableService.getTableNameByUUID(id);
        return this.baseMapper.selectUserMoneyAccountById(tableName,id);
    }

    @Override
    public UserMoneyAccount getUserMoneyAccountByUserId(String userId) throws GlobalExcetion {
        User user=userFeignService.getUserByHashUserId(userId);
        if(user==null){
            throw new GlobalExcetion(1,"用户账户不存在");
        }
        BusSystemTableUser busSystemTableUser =busSystemTableUserService.selectByUserId(user.getUuid(), PaySystemConstant.User_Money_tableName);
        UserMoneyAccount account= this.baseMapper.selectUserMoneyAccountByUserId(busSystemTableUser.getTableNode(),userId);
        if(account==null){
            account=createAccount(user);
        }else {
            Boolean isUpdate=false;
            if(user.getPhone()!=null){
                isUpdate=true;
                account.setUserPhone(user.getPhone());
            }
            if(user.getIdCard()!=null){
                isUpdate=true;
                account.setUserIdcard(user.getIdCard());
            }
            if(isUpdate){
                this.updateUserMoneyAccountById(account);
            }
        }
        return account;
    }

    @Override
    public UserMoneyAccount insertMoney(UserMoneyVo vo) throws GlobalExcetion {
        User user=userFeignService.getUserByHashUserName(vo.getUserName());
        if(user==null){
            throw new GlobalExcetion(1,"用户账户不存在");
        }
        UserMoneyAccount account=this.getUserMoneyAccountByUserId(user.getUuid());

        BigDecimal price = new BigDecimal(account.getUserMoney());
        BigDecimal insvertMoney=new BigDecimal(vo.getMoney());
        BigDecimal money= BigDecimalUtils.add(price,insvertMoney);
        account.setUserMoney(money.toString());
        this.updateUserMoneyAccountById(account);

        DataNode dataNode=nodeFeignService.getDataNodeConfigByServerName(appName);
        UserPayRecordIn userPayRecordIn=new UserPayRecordIn();
        userPayRecordIn.setFromUuid("system");
        userPayRecordIn.setToUuid(user.getUuid());
        userPayRecordIn.setDsNo(dataNode.getId());
        userPayRecordIn.setPayMoney(vo.getMoney());
        userPayRecordIn.setPayType("充值");
        userPayRecordIn.setPayNote("充值");
        userPayRecordInService.saveUserPayRecordIn(user.getUuid(),userPayRecordIn);
        return account;
    }

    @Override
    public UserMoneyAccount createAccount(User user) throws GlobalExcetion {
        DataNode dataNode=nodeFeignService.getDataNodeConfigByServerName(appName);
        BusSystemTableUser busSystemTableUser =busSystemTableUserService.selectByUserId(user.getUuid(), PaySystemConstant.User_Money_tableName);
        String tableName=busSystemTableUser.getSaveTableName();
        UserMoneyAccount account=new UserMoneyAccount();
        account.setUserId(user.getUuid());
        account.setUserMoney("0.00");
        account.setIsEnable(0);
        account.setUserName(user.getUsername());
        account.setPayPassword(user.getPassword());
        account.setDsNo(dataNode.getId());
        return this.saveUserMoneyAccount(tableName,account);
    }

    @Override
    public UserMoneyAccount info(User user) throws GlobalExcetion {
        UserMoneyAccount account=this.getUserMoneyAccountByUserId(user.getUuid());
        return account;
    }


}




